package com.itdct.mto.core.manager;

import com.itdct.mto.core.base.BaseManager;
import com.itdct.mto.core.bo.ColumnBo;
import com.itdct.mto.core.bo.TableBo;
import com.itdct.mto.core.context.ColumnContext;
import com.itdct.mto.core.context.Context;
import com.itdct.mto.core.context.TableContext;
import com.itdct.mto.core.manager.base.BaseDataTransferManager;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Zhouwx
 * @date 2025/7/2 10:13:39
 * @version 1.0
 * @description OpenGauss A类数据迁移（PostgreSQL模式）
 */
@ConditionalOnProperty(name = "mto.database-mode", havingValue = "A")
@Slf4j
@Component
public class ADataTransferManager extends BaseDataTransferManager {

    @Override
    protected void appendInsertLineSql(StringBuilder insertBuilder, ColumnContext columnContext) throws Exception {
        ResultSet rs = columnContext.getRs();
        String columnType = columnContext.getColumnType();
        String columnValue = columnContext.getColumnValue();
        boolean columnNotNull = columnContext.isNotNull();

        if (rs.wasNull()) {
            // INFO: Zhouwx: 2025/7/2 值为NULL，那就直接写入NULL
            insertBuilder.append("NULL");
        } else {
            if (columnType.toUpperCase().contains("INT")) {
                // INFO: Zhouwx: 2025/7/2 必须使用temp，不然后面连锁查询就坏了，截掉了一位会再次从前往后查，这样会重复insert之前的值！！
                String tempColumnValue = columnValue;
                if (columnType.contains("BIGINT")) {
                    if (tempColumnValue.length() > 19) {
                        // INFO: Zhouwx: 2025/7/2 防止id超过范围
                        int sub = tempColumnValue.length() - 19;
                        tempColumnValue = tempColumnValue.substring(sub);
                    }

                    if (tempColumnValue.length() == 19) {
                        // INFO: Zhouwx: 2025/7/2 有虫豸把雪花算法多了一位，导致bigint都溢出了！！
                        BigDecimal bigDecimalColumnValue = new BigDecimal(tempColumnValue);
                        BigDecimal maxLongBigDecimal = new BigDecimal(Long.MAX_VALUE);
                        if (bigDecimalColumnValue.compareTo(maxLongBigDecimal) > 0) {
                            // INFO: Zhouwx: 2025/7/2 超过long的最大值，直接截断1位
                            tempColumnValue = tempColumnValue.substring(1);
                        }
                    }
                }
                insertBuilder.append(tempColumnValue);
            } else {
                if (columnValue.isEmpty()) {
                    if (columnNotNull) {
                        // INFO: Zhouwx: 2025/7/2 值为空，且字段不能为空，需要填默认值进去
                        insertBuilder.append(mtoConfig.getDefaultEmptyNotNullColumnValue());
                    } else {
                        // INFO: Zhouwx: 2025/7/3 该字段可以为空，那就赋值空字符串吧
                        insertBuilder.append("''");
                    }
                } else {
                    // INFO: Zhouwx: 2025/7/2 字符串类型都需要转义，防止出现SQL报错
                    columnValue = columnValue.replace("'", "''")
                            .replace("\\", "\\\\")
                            .replace("\"", "\\\"");
                    insertBuilder.append("'").append(columnValue).append("'");
                }
            }
        }

    }
}
